import * as Cesium from '@cesiumjs';
import {
  autorun,
  reaction,
  intercept,
  observable,
  extendObservable,
  computed
} from 'mobx';
import * as g3xUtils from "@g3x/utils";

/* 246 */
/***/
function _typeof3(e){
  // var _typeof2 = __webpack_require__(36);
  return typeof(e)
}

function registerPathAttachedGuid(selfObject) {
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
    _ref$positionProperty = _ref.positionProperty,
    positionProperty = _ref$positionProperty === undefined ? 'position' : _ref$positionProperty,
    _ref$rotationProperty = _ref.rotationProperty,
    rotationProperty = _ref$rotationProperty === undefined ? 'rotation' : _ref$rotationProperty,
    _ref$attachedPathGuid = _ref.attachedPathGuidProperty,
    attachedPathGuidProperty = _ref$attachedPathGuid === undefined ? 'attachedPathGuid' : _ref$attachedPathGuid;

  if (typeof selfObject[attachedPathGuidProperty] === 'undefined') {
    extendObservable(selfObject, g3xUtils.defineProperty({}, attachedPathGuidProperty, ''));
  }

  var _attachedPathProperty = '_' + attachedPathGuidProperty + '_obj';

  extendObservable(selfObject, g3xUtils.defineProperty({}, _attachedPathProperty, undefined), g3xUtils
  .defineProperty({}, _attachedPathProperty, observable.ref));

  var disposers = [];

  disposers.push(reaction(function() {
    var attachedPath = selfObject[_attachedPathProperty];
    return {
      position: attachedPath && _typeof3(attachedPath.currentPosition) ? [...attachedPath.currentPosition] : undefined,
      rotation: attachedPath && _typeof3(attachedPath.currentRotation) ? [...attachedPath.currentRotation] : undefined
    };
  }, function(par) {
    if (par.position && selfObject[positionProperty]) {
      selfObject[positionProperty] = par.position;
    }

    if (par.rotation && selfObject[rotationProperty]) {
      if (!selfObject.isCameraLike) {
        selfObject[rotationProperty] = [par.rotation[0] - Cesium.Math.PI_OVER_TWO, par.rotation[1], par
          .rotation[2]
        ];
      } else {
        selfObject[rotationProperty] = par.rotation;
      }
    }
  }));

  disposers.push(selfObject._earth.registerObjectFromGuid(selfObject, attachedPathGuidProperty,
    _attachedPathProperty));

  return function() {
    disposers.forEach(function(d) {
      return d();
    });
    disposers.length = 0;
  };
}

export default registerPathAttachedGuid;
